package asn.match.dao;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import asn.match.model.Ressource;

@Repository("ressource")
@PersistenceContext(type = PersistenceContextType.EXTENDED)
@Transactional
public class RessourceDaoImpl implements RessourceDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addRessource(Ressource ressource) {
		Date date = new Date();
		Timestamp created_at = new Timestamp(date.getTime());
		ressource.setCreatedAt(created_at);
		this.sessionFactory.getCurrentSession().save(ressource);
	}

	@Override
	public void updateRessource(Ressource ressource) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(ressource);
	}

	@Override
	public void deleteRessource(Ressource ressource) {
		this.sessionFactory.getCurrentSession().delete(ressource);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ressource> findAllRessource() {
		return (List<Ressource>) this.sessionFactory.getCurrentSession().createQuery("from Ressource ressource").list();
	}

	@Override
	public Ressource findRessourceById(Integer ressourceId) {
		return (Ressource) this.sessionFactory.getCurrentSession().get(Ressource.class, ressourceId);
	}

}
